package com.ctsi.ssdc.service.impl;

import com.ctsi.ssdc.entity.Excel08VO.Excel15VO;
import com.ctsi.ssdc.entity.Excel15;
import com.ctsi.ssdc.entity.TradeAndServiceIndustry;
import com.ctsi.ssdc.mapper.Excel15Mapper;
import com.ctsi.ssdc.service.Excel15Service;
import com.ctsi.ssdc.utils.TimeUtil;
import org.springframework.data.domain.Pageable;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.concurrent.Future;

/**
 * 15基础表贸易分行业(Excel15)表服务实现类
 *
 * @author makejava
 * @since 2023-02-21 16:26:43
 */
@Service("excel15Service")
public class Excel15ServiceImpl implements Excel15Service {
    @Resource
    private Excel15Mapper excel15Dao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Excel15 queryById(Long id) {
        return this.excel15Dao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param excel15     筛选条件
     * @param pageRequest 分页对象
     * @return 查询结果
     */
    @Override
    public Page<Excel15> queryByPage(Excel15 excel15, Pageable pageRequest) {
        long total = this.excel15Dao.count(excel15);
        return new PageImpl<>(this.excel15Dao.queryAllByLimit(excel15, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param excel15 实例对象
     * @return 实例对象
     */
    @Override
    public Excel15 insert(Excel15 excel15) {
        this.excel15Dao.insert(excel15);
        return excel15;
    }

    /**
     * 修改数据
     *
     * @param excel15 实例对象
     * @return 实例对象
     */
    @Override
    public Excel15 update(Excel15 excel15) {
        this.excel15Dao.update(excel15);
        return this.queryById(excel15.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long id) {
        return this.excel15Dao.deleteById(id) > 0;
    }

    /**
     * 批量增加
     * @param list
     * @return
     */
    @Override
    public boolean insertBatch(List<Excel15> list) {
        return excel15Dao.insertBatch(list)>0;
    }

    @Override
    public List<Excel15VO> keyInfo(String year, String month) {
        return this.excel15Dao.keyInfo(year,month);
    }

    /**
     * 批零住餐营业额当月 + 累计 + 同比:（本期累计/同期累计-1）*100%
     * @param year
     * @param month
     * @return
     */
    @Override
    public TradeAndServiceIndustry getTurnoverTotal(String year, String month) {
        TradeAndServiceIndustry info = excel15Dao.getTurnoverTotal(year, month);
        if (info != null) {
            info.setStatisticalTime(TimeUtil.date(year, month));
        }
        return info;
    }

    /**
     * 批发业营业额当月 + 累计 + 同比:（本期累计/同期累计-1）*100%
     * @param year
     * @param month
     * @return
     */
    @Override
    public TradeAndServiceIndustry getWholesaleTurnoverTotal(String year, String month) {
        TradeAndServiceIndustry info = excel15Dao.getTurnoverTotalBySectorName(year, month,"批发业");
        if (info != null) {
            info.setIndicators("限上批发业销售额");
            info.setStatisticalTime(TimeUtil.date(year, month));
        }
        return info;
    }

    /**
     * 零售业营业额当月 + 累计 + 同比:（本期累计/同期累计-1）*100%
     * @param year
     * @param month
     * @return
     */
    @Override
    public TradeAndServiceIndustry getRetailTurnoverTotal(String year, String month) {
        TradeAndServiceIndustry info = excel15Dao.getTurnoverTotalBySectorName(year, month,"零售业");
        if (info != null) {
            info.setIndicators("限上零售业销售额");
            info.setStatisticalTime(TimeUtil.date(year, month));
        }
        return info;
    }

    /**
     * 住宿业营业额当月 + 累计 + 同比:（本期累计/同期累计-1）*100%
     * @param year
     * @param month
     * @return
     */
    @Override
    public TradeAndServiceIndustry getLodgingTurnoverTotal(String year, String month) {
        TradeAndServiceIndustry info = excel15Dao.getTurnoverTotalBySectorName(year, month,"住宿业");
        if (info != null) {
            info.setIndicators("限上住宿业营业额");
            info.setStatisticalTime(TimeUtil.date(year, month));
        }
        return info;
    }

    /**
     * 餐饮业营业额当月 + 累计 + 同比:（本期累计/同期累计-1）*100%
     * @param year
     * @param month
     * @return
     */
    @Override
    public TradeAndServiceIndustry getCateringTurnoverTotal(String year, String month) {
        TradeAndServiceIndustry info = excel15Dao.getTurnoverTotalBySectorName(year, month,"餐饮业");
        if (info != null) {
            info.setIndicators("限上餐饮业营业额");
            info.setStatisticalTime(TimeUtil.date(year, month));
        }
        return info;
    }

    /**
     * 根据日期删除
     * @param date
     * @return 是否成功
     */
    @Override
    public boolean removeByDate(Date date) {
        return excel15Dao.removeByDate(date);
    }
}
